package com.exam.controller.teacher;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.exam.common.vo.ExamTeacherVO;
import com.exam.common.vo.page.PageModel;
import com.exam.entity.ExamTeacher;
import com.exam.service.teacher.TeacherService;

@Controller
@RequestMapping("/tea")
public class TeacherController {

	@Autowired
	TeacherService teaService;
	
	@RequestMapping(value="/teacherList")
	public String list(Model model){
		PageModel<ExamTeacherVO>  page = teaService.findByPage();
		model.addAttribute("page",page);
		return "/teacher/list";
	}
	
	@RequestMapping(value="/delete/{teaNumber}")
	public String delete(@PathVariable String teaNumber){
		ExamTeacher teacher = new ExamTeacher();
		teacher.setExamTeacherId(Integer.parseInt(teaNumber));
		teaService.delete(teacher);
		return "redirect:/tea/teacherList";
	}
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String add(Model model){
		ExamTeacherVO teacherVO = new ExamTeacherVO();
		model.addAttribute("teacherVO",teacherVO);
		return "/teacher/add";
	}
	
	@RequestMapping(value="/add", method = RequestMethod.POST)
	public String save(ExamTeacherVO teacherVO,BindingResult br,RedirectAttributes redirectAttributes){
		if(br.hasErrors()){
			return "/teacher/add";
		}
		ExamTeacher teacher = new ExamTeacher();
		if(teacherVO!=null){
			BeanUtils.copyProperties(teacherVO, teacher);
			teaService.save(teacher);
			redirectAttributes.addFlashAttribute("message","添加成功");
		}
		return "redirect:/tea/teacherList";
	}
	
	
	@RequestMapping(value="/search", method = RequestMethod.POST)
	public String search(String searchTip,Model model){
		PageModel<ExamTeacherVO>  page = teaService.findBySearcherTip(searchTip);
		model.addAttribute("page",page);
		if(searchTip!=null | !searchTip.equals("")){
			model.addAttribute("searchTip",searchTip);
		}
		return "/teacher/list";
	}
	
}
